package cn.fengliu.controller.adm;


import cn.fengliu.Dao.LargeFileDao;
import cn.fengliu.Dao.UserDao;
import cn.fengliu.entity.LargeFile;
import cn.fengliu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

@Controller   //创建对象，并且装配到 IOC容器
@RequestMapping("/tea")
public class TeaController {
    @Autowired
    UserDao userDao;
    @Autowired
    LargeFileDao largeFileDao;
    public TeaController() {
        System.out.println("teaController 对象被创建了");
     //当前页数
    }
    @RequestMapping("/show")
    public String show(Model model,Integer curpage){

        int total = userDao.getTotal("tea");//总行数
        System.out.println(total);
        if (curpage==null){
            curpage=1; }//当前页数
        int curpagemax=total/6;//总页数
        System.out.println(curpagemax);
        System.out.println(curpage);
        if (curpagemax%6!=0){
            curpagemax=curpagemax+1;
        }
        System.out.println("总页数="+curpagemax);

//        List<User> users = userDao.findUser("tea");
        List<User> teas = userDao.findPage((curpage - 1)* 6,6,"tea");

//        User teas = userDao.findByid(2023060608);
        System.out.println(teas);
        model.addAttribute("users",teas);
        model.addAttribute("curpage" ,curpage);
        model.addAttribute("curpagemax",curpagemax);
        return "/adm/tea/show";
    }




    @RequestMapping("/nextpage")
    public  String nextpage(Integer curpage,Model model){
        if (curpage==null)curpage=1;
        int total = userDao.getTotal("tea");//总行数
        if (curpage==-1){
            curpage=total/6;
        }
        if (curpage*6<total)curpage++;
        System.out.println("total="+total);
        System.out.println("curpage="+curpage*6);
        model.addAttribute("User",new User());
        return "forward:/tea/show?curpage="+curpage;
    }
    @RequestMapping("/prevpage")
    public  String prevpage(Integer curpage,Model model){
        if (curpage==null)curpage=1;
        if (curpage>1)curpage--;
        model.addAttribute("User",new User());
        return "forward:/tea/show?curpage="+curpage;
    }



    @RequestMapping("/go2add")
    public String go2add(Model model){
        model.addAttribute("action","add");
        return "forward:/tea/show"; }

    @RequestMapping("/add")
        public String add(  User user,
                      @RequestParam(value = "img",
        required= true) MultipartFile filex ,Model model){
                    user.setRole("tea");
//        System.out.println("user"+user);
        System.out.println("filex"+filex);
        if (filex==null || filex.isEmpty()==true){
            System.out.println("没有图片");
            String s="姓名为"+user.getUname()+"没有插入头像";
            model.addAttribute("Session",s);
            return "forward:/tea/show";
        }
        String uuid = UUID.randomUUID().toString();
        try {
        LargeFile largeFile=new LargeFile();

        System.out.println("uuid="+uuid);
        largeFile.setId(uuid);
        largeFile.setFilename(filex.getOriginalFilename());
        byte[] buf= new byte[0];

            buf = filex.getBytes();
        System.out.println("buf.length="+buf.length);
        largeFile.setContent(buf);
//            System.out.println("largeFile="+largeFile);
        largeFileDao.add(largeFile);

    } catch (Exception e) {
            System.out.println(e.getMessage());
       return "err";
        }
        user.setPic(uuid);
//        System.out.println(uuid);
        System.out.println("user"+user);
       userDao.add(user);
        return "forward:/tea/nextpage?curpage=-1";


    }

    @RequestMapping("/go2update/{uid}")
    public String go2update(@PathVariable("uid") Integer uid,  Model model,Integer curpage){
        if (curpage==null)curpage=1;
        User use= userDao.findByid(uid);
        use.setRole("tea");
        System.out.println(use);
        model.addAttribute("use",use);
        model.addAttribute("action","update");
        return "forward:/tea/show?curpage="+curpage;}

//    @RequestParam(value = "img", required= false)
//   required= false  图片可有可无      required=true 图片一要有

        @RequestMapping("/update")
        public String update(User user,
                    @RequestParam(value = "img",
                            required=false) MultipartFile filex){

            user.setRole("tea");
        System.out.println("user"+user);
            System.out.println("filex"+filex);
//            判断 前端 数据是否有 添加新的图片   如果没有 插入图片 执行 if语句 并返回
            if (filex==null || filex.isEmpty()==true){
                System.out.println("没有图片");
                userDao.update(user);
                return "forward:/tea/show";
            }

//          根据 前端 给的 uid信息 查找 该用户的pic 并做删除 旧的pic
            User tea = userDao.findBypic(user.getUid());
            System.out.println(tea.getPic());
            largeFileDao.del(tea.getPic());

            String uuid = UUID.randomUUID().toString();
            try {
                LargeFile largeFile=new LargeFile();

                System.out.println("uuid1="+uuid);
                largeFile.setId(uuid);
                largeFile.setFilename(filex.getOriginalFilename());
                byte[] buf= new byte[0];

                buf = filex.getBytes();
                System.out.println("buf.length="+buf.length);
                largeFile.setContent(buf);
            System.out.println("largeFile="+largeFile);
                largeFileDao.add(largeFile);

            } catch (Exception e) {
                System.out.println(e.getMessage());
                return "err";
            }
            user.setPic(uuid);
        System.out.println(uuid);
            System.out.println("user"+user);
            userDao.update(user);
            return "forward:/tea/show";
        }

        @RequestMapping("/del/{uid}")
        public String del(@PathVariable("uid") Integer uid ,Model model){
            User tea = userDao.findBypic(uid);
            largeFileDao.del(tea.getPic());
//            System.out.println(uid);
            userDao.del(uid);
            String s="uid为"+uid+"的教师，以删除";
            model.addAttribute("Session",s);
            return "forward:/tea/show";
        }
    @RequestMapping("/ok")
  public String ok(){
        User teas = userDao.findByid(2023060608);
        System.out.println(teas);
        return "ok";
  }

}
